package com.example.baidusupport;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;

import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.InfoWindow;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.Overlay;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionSearch;
import com.baidu.mapapi.search.sug.SuggestionSearchOption;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private MapView mMapView = null;
    private BaiduMap mBaiduMap;
    private SearchView searchView;
    private SuggestionSearch mSuggestionSearch;
    private ListView listView;
    private ArrayAdapter<String> arrayAdapter;
    private List<SuggestionResult.SuggestionInfo> suggestionInfos;
    private List<Overlay> overlays;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mMapView = (MapView) findViewById(R.id.bmapView);
        Log.e("sss", "加载完成");
        mBaiduMap = mMapView.getMap();
        overlays=new ArrayList<>();

        //百度地图搜索监听
        initListView();
        initBaiduSearch();
        initSearchView();
    }

    //初始化列表
    private void initListView() {
        listView = findViewById(R.id.listView);
        List<String> suggestions = new ArrayList<>();
        arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, suggestions);
        listView.setAdapter(arrayAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                listView.setVisibility(View.INVISIBLE);

                SuggestionResult.SuggestionInfo row = suggestionInfos.get(i);
                MapStatus.Builder builder = new MapStatus.Builder();
                builder.target(row.getPt()).zoom(17);
                mBaiduMap.animateMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));
            }
        });
    }

    //初始化搜索工具
    private void initSearchView() {
        searchView = findViewById(R.id.searchView);
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String s) {
                mSuggestionSearch.requestSuggestion(new SuggestionSearchOption()
                        .city("北京")
                        .keyword(s));

                Log.e("点击搜索", s);
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                Log.e("文字改变", s);
                return false;
            }
        });
    }

    //初始化百度搜索API
    private void initBaiduSearch() {
        mSuggestionSearch = SuggestionSearch.newInstance();

        OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {
            @Override
            public void onGetSuggestionResult(SuggestionResult suggestionResult) {
                //显示列表视图
                listView.setVisibility(View.VISIBLE);
                //清楚上次地图点结果
                mBaiduMap.removeOverLays(overlays);
                //处理sug检索结果
                suggestionInfos = suggestionResult.getAllSuggestions();
                List<String> keys = new ArrayList<>();
                if (suggestionInfos != null && suggestionInfos.size() > 0) {
                    for (int i = 0; i < suggestionInfos.size(); i++) {
//                        Log.e("百度搜索结果",result.toString());
                        keys.add(suggestionInfos.get(i).getKey());
                        addPoint(suggestionInfos.get(i).getPt(), suggestionInfos.get(i).getKey());
                    }
                }
                arrayAdapter.clear();
                arrayAdapter.addAll(keys);
                arrayAdapter.notifyDataSetChanged();
            }
        };
        mSuggestionSearch.setOnGetSuggestionResultListener(listener);
    }

    //在地图上添加点
    private void addPoint(LatLng point, String name) {
        //无坐标不展示
        if (point == null) {
            return;
        }
        BitmapDescriptor bitmap = BitmapDescriptorFactory
                .fromResource(R.drawable.locate);

        //用来构造InfoWindow的Button
        TextView mInfo = new TextView(getApplicationContext());
        mInfo.setBackgroundColor(Color.WHITE);
        mInfo.setTextColor(Color.BLACK);
        mInfo.setText(name);

//构造InfoWindow
//point 描述的位置点
//-100 InfoWindow相对于point在y轴的偏移量
        InfoWindow mInfoWindow = new InfoWindow(mInfo, point, -100);
//构建MarkerOption，用于在地图上添加Marker
        OverlayOptions option = new MarkerOptions()
                .position(point)
                .infoWindow(mInfoWindow)
                .icon(bitmap);

//在地图上添加Marker，并显示
        Overlay overlay = mBaiduMap.addOverlay(option);
        overlays.add(overlay);

    }

}